/*
 * FileName：MsgNewsServiceImpl.java 
 * <p>
 * Copyright (c) 2017-2020 <a href="https://www.smartwx.info">hermit(1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.smart.wx.wxcms.service.impl;

import com.smart.common.util.WxMemoryCacheClient;
import com.smart.wx.wxcms.entity.MediaFiles;
import com.smart.wx.wxcms.entity.MsgArticle;
import com.smart.wx.wxcms.entity.MsgBase;
import com.smart.wx.wxcms.entity.MsgNews;
import com.smart.wx.wxcms.dao.MediaFilesMapper;
import com.smart.wx.wxcms.dao.MsgArticleMapper;
import com.smart.wx.wxcms.dao.MsgBaseMapper;
import com.smart.wx.wxcms.dao.MsgNewsMapper;
import com.smart.wx.wxcms.service.MsgNewsService;
import com.smart.wx.wxapi.process.MsgType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author hermit
 * @version 2.0
 * @date 2018-04-17 10:54:58
 */
@Service
@Transactional
public class MsgNewsServiceImpl implements MsgNewsService {

	@Resource
	private MsgBaseMapper msgBaseMapper;

	@Resource
	private MsgNewsMapper msgNewsMapper;

	@Resource
	private MediaFilesMapper mediaFilesMapper;
	
	@Resource
	private MsgArticleMapper msgArticleMapper;

	public MsgNews getById(String id){
		return msgNewsMapper.getById(id);
	}

	public List<MsgNews> listForPage(MsgNews searchEntity){
		return msgNewsMapper.listForPage(searchEntity);
	}
	
	public List<MsgNews> getWebNewsListByPage(MsgNews searchEntity){
		return msgNewsMapper.getWebNewsListByPage(searchEntity);
	}

	
	public void add(MsgNews entity){
		
		MsgBase base = new MsgBase();
		base.setInputcode(entity.getInputcode());
		base.setCreateTime(new Date());
		base.setMsgtype(MsgType.News.toString());
		base.setAccount(WxMemoryCacheClient.getAccount());
		msgBaseMapper.add(base);
		
		entity.setBaseId(base.getId());
		msgNewsMapper.add(entity);
		
		if(StringUtils.isEmpty(entity.getFromurl())){
			entity.setUrl(entity.getUrl()+"?id="+entity.getId());
		}else{
			entity.setUrl("");
		}
		
		msgNewsMapper.updateUrl(entity);
	}

	public void update(MsgNews entity){
		MsgBase base = msgBaseMapper.getById(entity.getBaseId().toString());
		base.setInputcode(entity.getInputcode());
		msgBaseMapper.updateInputcode(base);
		
		if(StringUtils.isEmpty(entity.getFromurl())){
			entity.setUrl(entity.getUrl()+"?id="+entity.getId());
		}else{
			entity.setUrl("");
		}
		
		msgNewsMapper.update(entity);
	}

	public void delete(MsgNews entity){
		MsgBase base = new MsgBase();
		base.setId(entity.getBaseId());
		msgArticleMapper.deleteByBatch(entity.getId().intValue());
		msgNewsMapper.delete(entity);
		msgBaseMapper.delete(entity);
		
	}

	public List<MsgNews> getRandomMsg(String inputCode,Integer num){
		return msgNewsMapper.getRandomMsgByContent(inputCode,num);
	}
	
	public MsgNews getByBaseId(String baseid){
		return msgNewsMapper.getByBaseId(baseid);
	}

	/* (non-Javadoc)
	 * @see MsgNewsService#updateMediaId(MsgNews)
	 */
	@Override
	public int updateMediaId(MsgNews entity) {
		int n = 0 ;
		try {
			msgNewsMapper.updateMediaId(entity);
			n = 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return n;
	}


	@Transactional
	@Override
	public void addSingleNews(MsgNews news, MediaFiles entity) {
		//保存基本消息
		MsgBase base = new MsgBase();
		base.setCreateTime(new Date());
		base.setMsgtype(MsgType.News.toString());
		base.setAccount(WxMemoryCacheClient.getAccount());
		msgBaseMapper.add(base);
		//保存图文信息
		news.setCreateTime(new Date());
		news.setBaseId(base.getId());
		news.setAccount(WxMemoryCacheClient.getAccount());
		this.msgNewsMapper.addNews(news);
		MsgArticle art = new MsgArticle();
		art.setAuthor(news.getAuthor());
		art.setContent(news.getDescription());
		art.setContentSourceUrl(news.getFromurl());
		art.setDigest(news.getBrief());
		art.setMediaId(news.getMediaId());
		art.setNewsIndex(0);
		art.setPicUrl(news.getPicpath());
		art.setShowCoverPic(news.getShowpic());
		art.setThumbMediaId(news.getThumbMediaId());
		art.setTitle(news.getTitle());
		art.setUrl(news.getUrl());
		art.setBaseId(base.getId());
		art.setNewsId(news.getId().intValue());
		art.setAccount(WxMemoryCacheClient.getAccount());
		art.setFansComment(news.getFanscancomment());
		art.setOpenComment(news.getOpencomment());
		msgArticleMapper.add(art);
		entity.setAccount(WxMemoryCacheClient.getAccount());
		entity.setBaseId(base.getId());
		mediaFilesMapper.add(entity);
	}
	
	/* (non-Javadoc)
	 * @see MsgNewsService#getMediaFileList()
	 */
	@Override
	public List<MediaFiles> getMediaFileList() {
		return this.mediaFilesMapper.getMediaFileList();
	}

	/* (non-Javadoc)
	 * @see MsgNewsService#getMsgNewsList()
	 */
	@Override
	public List<MsgNews> getMsgNewsList() {
		return this.msgNewsMapper.getMsgNewsList();
	}

	/* (non-Javadoc)
	 * @see MsgNewsService#addMoreNews(MsgNews)
	 */
	@Transactional
	@Override
	public void addMoreNews(MsgNews news) {
		List<MsgArticle> articles = news.getArticles();
		List<MsgArticle> list = new ArrayList<MsgArticle>();
		//保存基本消息
		MsgBase base = new MsgBase();
		base.setCreateTime(new Date());
		base.setMsgtype(MsgType.News.toString());
		base.setAccount(WxMemoryCacheClient.getAccount());
		msgBaseMapper.add(base);
		news.setBaseId(base.getId());
		news.setCreateTime(new Date());
		news.setAccount(WxMemoryCacheClient.getAccount());
		//保存图文信息
		this.msgNewsMapper.addNews(news);
		for (int i = 0; i < articles.size(); i++) {
			MsgArticle article = articles.get(i);
			article.setNewsId(news.getId().intValue());
			article.setBaseId(base.getId());
			article.setAccount(WxMemoryCacheClient.getAccount());
			list.add(article);
		}
		msgArticleMapper.insertByBatch(list);
		MediaFiles entity = new MediaFiles();
		entity.setMediaId(news.getMediaId());
		entity.setMediaType("news");
		entity.setCreateTime(news.getCreateTime());
		entity.setUpdateTime(news.getCreateTime());
		entity.setAccount(WxMemoryCacheClient.getAccount());
		entity.setBaseId(base.getId());
		mediaFilesMapper.add(entity);
	}

	/* (non-Javadoc)
	 * @see MsgNewsService#deleteNews(java.lang.String)
	 */
	@Override
	public void deleteNews(String mediaId) {
		this.mediaFilesMapper.deleteByMediaId(mediaId);
		this.msgNewsMapper.deleteByMediaId(mediaId);
	}

	/* (non-Javadoc)修改单图文
	 * @see MsgNewsService#updateSingleNews(MsgNews)
	 */
	@Transactional
	@Override
	public void updateSingleNews(MsgNews news) {
    	MsgArticle art = new MsgArticle();
		art.setAuthor(news.getAuthor());
		art.setContent(news.getDescription());
		art.setContentSourceUrl(news.getFromurl());
		art.setDigest(news.getBrief());
		art.setMediaId(news.getMediaId());
		art.setNewsIndex(0);
		art.setPicUrl(news.getPicpath());
		art.setShowCoverPic(news.getShowpic());
		art.setThumbMediaId(news.getThumbMediaId());
		art.setTitle(news.getTitle());
		art.setUrl(news.getUrl());
		art.setFansComment(news.getFanscancomment());
		art.setOpenComment(news.getOpencomment());
		art.setNewsId(news.getId().intValue());
		int arId= msgArticleMapper.getByNewsId(news.getId().intValue()).get(0).getArId();
		art.setArId(arId);
		msgArticleMapper.update(art);
		this.msgNewsMapper.updateNews(news);
	}

	/* (non-Javadoc)
	 * @see MsgNewsService#getByMediaId(java.lang.String)
	 */
	@Override
	public List<MsgNews> getByMediaId(String mediaId) {
		return this.msgNewsMapper.getByMediaId(mediaId);
	}


	
}
